package com.github.shiqiyue.blog.markdown.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import com.github.shiqiyue.blog.markdown.entity.Article;
import com.github.shiqiyue.blog.markdown.repository.ArticleRepository;

/***
 * article service
 * 
 * @author wwy
 *
 */
@CacheConfig(cacheNames = "cache.article")
@Service
public class ArticleService {
	@Autowired
	private ArticleRepository articleRepository;
	
	@CacheEvict(allEntries = true)
	public Article save(Article article) {
		return articleRepository.save(article);
	}
	
	@Cacheable
	public Article findById(Long id) {
		return articleRepository.findOne(id);
	}
	
	@Cacheable
	public Page<Article> page(Pageable pageable, Example<Article> example) {
		return articleRepository.findAll(example, pageable);
	}
	
	@CacheEvict(allEntries = true)
	public void delete(Article article) {
		articleRepository.delete(article);
	}
	
}
